<?php
namespace V1\Controller;


class TopicController extends BaseController {
    
    protected $errorCode = array(
        'index' => array(
            '-10'=>'数据不存在',
        ) ,
        'detail' => array(
            '-10'=>'数据不存在',
        ) ,
        'bookmark' => array(
            '-1'=>'用户token失效或有误',
            '-10'=>'添加不成功',
            '-11'=>'已添加成功，请勿重复操作',
        ),
    );
    /**
     * 话题列表
     * 
     */
    public function index(){
       $data = array("code" => 0,"msg" => "","info"=>array());
       $field = array('id','title','cover_img','bookmark','update_time');
       $topic_list   = $this->lists('Topic',array('display'=>1),'level asc,id desc', array('status'=>array('egt',1)),$field);
       
       empty($topic_list['list']) && $this->responseError(-10);
   
       $data['info'] = $topic_list; 
       $this->responseResult($data);
    }
    /**
     * 消息
     */
    public function detail(){
      
      $data = array("code" => 0,"msg" => "","info"=>array());
      $topic_id = intval(I('id','0'));
      if(empty($topic_id)){
            throw new \Api\Library\Exception\BadRequest("参数异常", 1);
       }
      $result =M('TopicDetail')->alias('a')->where("a.id={$topic_id}")->join('mt_topic b on a.id=b.id')->field('a.id,a.industry_codes,b.title,a.content')->find();
      empty($result) && $this->responseError(-10);
      
      $data['info'] = $result; 
      
      $this->responseResult($data);
    }
    /**
     * 用户收藏话题
     */
    public function bookmark(){
      $data = array("code" => 0,"msg" => "","info"=>array());
      
      $topic_id = I('id',0);
      
      if(empty($topic_id)){
            throw new \Api\Library\Exception\BadRequest("参数异常", 1);
       }
       
      list($uid,$user_token) = $this->checkUserToken();
      
      
      $userTopicModel = M('UserTopic');
      
      $count = $userTopicModel->where(array('user_id' => $uid,'topic_id' => $topic_id,'status'=>1))->count();
      !empty($count) && $this->responseError(-11);
      
      $map = ['user_id' => $uid,'topic_id' => $topic_id,'create_time' => NOW_TIME,'update_time' => NOW_TIME,];
      if($userTopicModel->create($map)){
            $userTopicModel->add();
            M('Topic')->where(array('topic_id' => $topic_id))->setInc('bookmark',1);
            $data['msg']='添加成功';
            $this->responseResult($data);
      } else {
            $this->responseError(-10);
      }
    }
    
}